import axios from '@/utils/request'
import { getStorage } from '../utils/storage'

// 用户认证（登录注册）
export const loginAPI = ({ mobile, code }) =>
  axios({
    url: '/v1_0/authorizations',
    method: 'POST',
    data: {
      mobile,
      code
    }
  })

// 刷新用户token
export const getNewTokenAPI = () =>
  axios({
    url: '/v1_0/authorizations',
    method: 'PUT',
    headers: {
      Authorization: 'Bearer ' + getStorage('refresh_token')
    }
  })

// 获取所有列表信息
export const getAllChannelsAPI = () =>
  axios({
    url: '/v1_0/channels',
    method: 'GET'
  })

// 获取用户列表信息
export const getUserChannelsAPI = () =>
  axios({
    url: '/v1_0/user/channels'
  })

// 设置用户的频道（重置式）
export const updateChannelsAPI = ({ channels }) =>
  axios({
    url: '/v1_0/user/channels',
    method: 'PUT',
    data: {
      channels
    }
  })

// 删除指定用户频道
export const removeTheChannelAPI = ({ target }) =>
  axios({
    url: `/v1_0/user/channels/${target}`,
    method: 'DELETE'
  })

// 获取文章新闻推荐
export const getAllArticleListAPI = ({ channel_id, timestamp }) =>
  axios({
    url: '/v1_0/articles',
    params: {
      channel_id,
      timestamp
    }
  })

// 反馈不喜欢的文章
export const dislikeArticleAPI = ({ target }) =>
  axios({
    url: '/v1_0/article/dislikes',
    method: 'POST',
    data: {
      target
    }
  })

// 反馈垃圾内容
export const reportArticleAPI = ({ target, type }) =>
  axios({
    url: '/v1_0/article/reports',
    method: 'POST',
    data: {
      target,
      type,
      remark: '我们没用到'
    }
  })
// 获取联想建议（自动补全）
export const searchSuggestAPI = ({ keywords }) =>
  axios({
    url: '/v1_0/suggestion',
    params: {
      q: keywords
    }
  })
// 获取搜索结果
export const searchResultAPI = ({ page = 1, per_page = 10, q }) =>
  axios({
    url: '/v1_0/search',
    params: {
      page,
      per_page,
      q
    }
  })
// 获取新闻详情
export const detailAPI = ({ article_id }) =>
  axios({
    url: `/v1_0/articles/${article_id}`
  })
// 取消关注用户
export const userUnFollwedAPI = ({ target }) =>
  axios({
    url: `/v1_0/user/followings/${target}`,
    method: 'DELETE'
  })
// 关注用户
export const userFollwedAPI = ({ target }) =>
  axios({
    url: '/v1_0/user/followings',
    method: 'POST',
    data: {
      target
    }
  })
// 对文章点赞
export const likeArticleAPI = ({ target }) =>
  axios({
    url: '/v1_0/article/likings',
    method: 'POST',
    data: {
      target
    }
  })
// 取消对文章点赞
export const unLikeArticleAPI = ({ target }) =>
  axios({
    url: `/v1_0/article/likings/${target}`,
    method: 'DELETE'
  })
// 获取评论或评论回复
export const commentsListAPI = ({ id, offset = null, limit = 10 }) =>
  axios({
    url: '/v1_0/comments',
    params: {
      type: 'a',
      source: id,
      offset,
      limit
    }
  })
// 对评论或评论回复点赞
export const commentsLikeAPI = ({ target }) =>
  axios({
    url: '/v1_0/comment/likings',
    method: 'POST',
    data: {
      target
    }
  })
// 取消对评论或评论回复点赞
export const commentsUnLikeAPI = ({ target }) =>
  axios({
    url: `/v1_0/comment/likings/${target}`,
    method: 'DELETE'
  })
// 对文章或者评论进行评论 -------------
export const commentSendAPI = ({ target, content, art_id = null }) => {
  const obj = {
    target,
    content
  }
  if (art_id !== null) {
    obj.art_id = art_id
  }
  return axios({
    url: '/v1_0/comments',
    method: 'POST',
    data: obj
  })
}
// 获取用户自己信息 (我的页面 显示数据)
export const getUserInfoAPI = () =>
  axios({
    url: '/v1_0/user'
  })
// 获取用户个人资料(编辑也面  显示数据)
export const userProfileAPI = () =>
  axios({
    url: '/v1_0/user/profile'
  })
// 编辑用户照片资料（头像、身份证照片）
export const updateUserPhotoAPI = (fd) =>
  axios({
    // Conten-Type: application/json; axios携带的,前提: data请求体是对象-> json字符串 ->发给后台
    // Content-Type: multipart/form-data; 浏览器携带的, 前提: data请求体必须是FormData类型对象
    url: '/v1_0/user/photo',
    method: 'PATCH',
    data: fd
  })
// 编辑用户个人资料 ----------------
export const updateUserProfileAPI = (theObj) => {
  const obj = {
    name: '',
    gender: 0,
    birthday: '',
    intro: ''
  }
  for (const prop in obj) {
    if (theObj[prop] === undefined) {
      delete obj[prop]
    } else {
      obj[prop] = theObj[prop]
    }
  }
  return axios({
    url: '/v1_0/user/profile',
    method: 'PATCH',
    data: obj
  })
}
